package com.weiyun.notebook

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import com.weiyun.notebook.Dao.NoteRepository
import com.weiyun.notebook.Database.AppDatabase
import com.weiyun.notebook.Entity.Note
import kotlinx.coroutines.*

class NoteViewModel(application: Application) : AndroidViewModel(application) {
    private val repository: NoteRepository
    val allNotes: LiveData<List<Note>>
//    val speNotes: LiveData<List<Note>>
    init {
        val noteDao = AppDatabase.getDatabase(application).NoteDao()
        repository = NoteRepository(noteDao)
        allNotes = repository.allNotes

    }
     fun getSpeNotes(content:String): LiveData<List<Note>>{
         return repository.getSpeNotes("%$content%")
    }
    fun insert(note: Note) = CoroutineScope(Job()).launch {
        repository.insert(note)
    }

    fun update(note: Note) = CoroutineScope(Job()).launch {
        repository.update(note)
    }

    fun delete(note: Note) = CoroutineScope(Job()).launch {
        repository.delete(note)
    }
}